package com.tangxz.chat.modules.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tangxz.chat.modules.user.entity.TUser;
import com.tangxz.chat.modules.user.mapper.TuserMapper;
import com.tangxz.chat.modules.user.service.ITuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description: 用户表
 * @Author: 唐小尊
 * @Date: 2021-04-20
 * @Version: V1.0
 */
@Service
public class TuserServiceImpl extends ServiceImpl<TuserMapper, TUser> implements ITuserService {
    @Autowired
    TuserMapper tuserMapper;

    @Override
    public TUser login(TUser tuser) {
        String userId = tuser.getId();
        String password = tuser.getPassword();
        //得到盐值
        tuser = this.getOne(new QueryWrapper<TUser>().eq("id", userId).eq("password",password));
//        String salt = tuser.getSalt();
        //如果加密后的密码等于数据库的密码，则成功！！

        return tuser;
    }

    @Override
    public List<TUser> getAllUser(String userId) {
        return tuserMapper.getAllUser(userId);
    }

    @Override
    public List<TUser> page(Map<String, Object> params) {
        try {
            Integer page = (Integer) params.get("page");
            Integer num = (Integer) params.get("num");
            Integer count = this.count();
            int startI = (page - 1) * num;
            int endI = page * num;
            endI = endI > count ? count : endI;
            return this.list(new QueryWrapper<TUser>().last("limit " + startI + " " + endI));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
